package com.gyp.controller.video;

import cn.hutool.json.JSONObject;
import com.egzosn.pay.common.bean.PayOrder;
import com.gyp.dto.pay.CreateOrderDTO;
import com.gyp.dto.pay.WxNotifyDTO;
import com.gyp.entity.GyPayOrder;
import com.gyp.model.common.PageInfo;
import com.gyp.model.common.Result;
import com.gyp.model.common.Search;
import com.gyp.service.GyPayOrderService;
import com.gyp.utils.WxPayJsApiHttpUtil;
import com.gyp.utils.WxPayJsApiNotifyUtil;
import com.gyp.vo.pay.CreateOrderVO;
import com.gyp.vo.pay.WxPayJsApiPreVO;
import com.wechat.pay.contrib.apache.httpclient.exception.HttpCodeException;
import com.wechat.pay.contrib.apache.httpclient.exception.NotFoundException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;

@Api(tags = "支付")
@RestController
@RequestMapping("/pay")
@RequiredArgsConstructor
public class PayController {

    private final WxPayJsApiHttpUtil wxPayJsApiHttpUtil;
    private final WxPayJsApiNotifyUtil wxPayJsApiNotifyUtil;

    private final GyPayOrderService payService;


    @ApiOperation("JSAPI支付创建订单")
    @GetMapping("/jsapi/create")
    public Result<WxPayJsApiPreVO> test() throws Exception {
        return Result.data(wxPayJsApiHttpUtil.jsApiV3());
    }

    @ApiOperation(("JSAPI支付查询"))
    @GetMapping("/queryJsApiOrderInfo")
    public Result<JSONObject> queryJsApiOrderInfo(@RequestParam("transactionId") String transactionId) throws URISyntaxException, GeneralSecurityException, IOException, NotFoundException, HttpCodeException {
        return Result.data(wxPayJsApiHttpUtil.queryJsApiOrderInfoV3(transactionId));
    }


    @ApiOperation(("JSAPI支付回调"))
    @PostMapping("/jsapi/notify")
    public void wxPayResult(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("微信公众号支付异步消息");
        String out_trade_no = wxPayJsApiNotifyUtil.notifyV3(request, response);
        System.out.println("out_trade_no:" + out_trade_no);
    }

    @ApiOperation("创建微信支付订单[native支付]")
    @PostMapping("/wx/order")
    public Result<CreateOrderVO> createWxPayOrder(@RequestBody @Valid CreateOrderDTO param) {
        return Result.data(payService.wxPayCreate(param));
    }

    @ApiOperation("查询订单详情")
    @PostMapping("/order/detail")
    public Result<GyPayOrder> selectOrderDetail(@RequestParam("orderId") Long orderId) {
        return Result.data(payService.selectOrderDetail(orderId));
    }

    @ApiOperation("查询订单列表")
    @PostMapping("/order/list")
    public Result<PageInfo<GyPayOrder>> selectOrders(Search search) {
        return Result.data(payService.selectOrders(search));
    }

    @ApiOperation("微信支付回调[native支付]")
    @PostMapping("/wx/notify")
    public ResponseEntity wxNotify(@RequestBody @Valid WxNotifyDTO param
    ) {
        payService.wxNotifyHandler(param);
        return new ResponseEntity(HttpStatus.OK);
    }




}
